package com.pjb.springbootjjwt.store.storage.fun;

import com.pjb.springbootjjwt.store.Defines;
import com.pjb.springbootjjwt.store.entity.Error;
import com.pjb.springbootjjwt.store.entity.*;
import com.pjb.springbootjjwt.store.storage.DML;
import com.pjb.springbootjjwt.store.storage.Handler;
import com.pjb.springbootjjwt.store.utils.FileUtil;
import com.pjb.springbootjjwt.store.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class FunctionHandlerImpl implements Handler {
    private final DML dml = new DML();

    @Override
    public void crateTable(String table, String ddl) throws Exception {
        String path = FileUtil.getPath(table);
        String sql = StringUtil.sub(ddl, "begin", "end");
        FileUtil.write(path, sql);

        ddl += " engine fun ";
        Define define = Define.builder().ddl(ddl).engine("fun").build();//new Define(ddl, "fun");
        define.setFields(new ArrayList<>());
        FileUtil.write(path + Defines.EXT_FRM, Define.toJson(define));
    }

    @Override
    public void dropTable(String table) throws Exception {
        FileUtil.removeFile(table + Defines.EXT_FRM);
        FileUtil.removeFile(table);
    }

    @Override
    public void delete(String table, List<Line> list, List<Line> sub) throws Exception {
        throw new Exception("" + Response.error.set(com.pjb.springbootjjwt.store.entity.Error.E90010, "delete"));
    }

    @Override
    public Record get(String table) throws Exception {
        String c = FileUtil.read(table);
        String s = FunctionUtil.analysis(FunctionUtil.parse(c));
        List<Line> list = new ArrayList<>();
        list.add(Line.toLine(s));
        return Record.builder().head(new Head()).line(list).build();//new Record(new Head(), list);
    }

    @Override
    public void saveList(String table, List<Line> list) throws Exception {
        throw new Exception("" + Response.error.set(Error.E90010, "saveList"));
    }
}
